package com.bytedance.danmaku.render.engine.demo.danmu.bordertext

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.Log
import com.bytedance.danmaku.render.engine.control.DanmakuConfig
import com.bytedance.danmaku.render.engine.data.DanmakuData
import com.bytedance.danmaku.render.engine.demo.danmu.project.GradientChangeDiggDanmaku
import com.bytedance.danmaku.render.engine.demo.danmu.project.GradientChangeDiggDanmakuDrawItem
import com.bytedance.danmaku.render.engine.demo.utils.DRAW_TYPE_BORDER_TEXT
import com.bytedance.danmaku.render.engine.demo.utils.DensityUtils.dp2px
import com.bytedance.danmaku.render.engine.demo.utils.DensityUtils.sp2px
import com.bytedance.danmaku.render.engine.demo.utils.dp
import com.bytedance.danmaku.render.engine.render.draw.DrawItem
import com.bytedance.danmaku.render.engine.render.draw.IDrawItemFactory
import com.bytedance.danmaku.render.engine.render.draw.text.TextDrawItem

/**
 * @Description:自定义有外边框的弹幕
 * @Author yanxin
 * @Date:  2025/8/20
 */

class BorderDanmaku : DanmakuData() {
    var gradientChangeDiggDanmaku: GradientChangeDiggDanmaku? = null
    override var drawType: Int = DRAW_TYPE_BORDER_TEXT
}

class BorderDanmakuDrawItem : DrawItem<BorderDanmaku>() {
    private val mBorderBgRectF = RectF()
    private val mBorderBgPaint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
    private var mPaddingLeft = dp2px(10F)  //左间距
    private var mPadding = dp2px(5F) * 2
    private var mPaddingRight = dp2px(3F) //右间距
    private var mMargin=dp2px(50f) //弹幕横向间隔
    // 创建临时画笔获取文字度量
    val paint = Paint().apply {
        textSize = sp2px(16F).toFloat()
    }

    val fontMetrics = paint.fontMetrics
    override var x: Float = 0F
        set(value) {
            field = value
            updateXY()
        }

    override var y: Float = 0F
        set(value) {
            field = value-20 //矫正轨道
            updateXY()
        }

    private val gradientChangeDiggDanmakuDrawItem: GradientChangeDiggDanmakuDrawItem =
        GradientChangeDiggDanmakuDrawItem()

    private fun updateXY() {
        gradientChangeDiggDanmakuDrawItem.x = x
//        gradientChangeDiggDanmakuDrawItem.y = y + mPadding
        gradientChangeDiggDanmakuDrawItem.y =y + (height - gradientChangeDiggDanmakuDrawItem.height-mPadding) / 2-(fontMetrics.ascent + fontMetrics.descent) / 2
    }

    override fun getDrawType(): Int {
        return DRAW_TYPE_BORDER_TEXT
    }

    override fun onMeasure(config: DanmakuConfig) {
        gradientChangeDiggDanmakuDrawItem.measure(config)
        width = gradientChangeDiggDanmakuDrawItem.width + mPaddingLeft+mMargin
        height = gradientChangeDiggDanmakuDrawItem.height+mPadding
        updateXY()
    }

    override fun onDraw(canvas: Canvas, config: DanmakuConfig) {
        drawDiggBg(canvas, config)
        gradientChangeDiggDanmakuDrawItem.draw(canvas, config)
    }

    override fun onBindData(data: BorderDanmaku) {
        data.gradientChangeDiggDanmaku?.let {
            gradientChangeDiggDanmakuDrawItem.bindData(it)
        }
    }

    private fun drawDiggBg(canvas: Canvas, config: DanmakuConfig) {
        mBorderBgPaint.setStyle(Paint.Style.STROKE); // 设置绘制样式为描边
        mBorderBgPaint.setStrokeWidth(3f);  // 设置边框宽度
        mBorderBgPaint.setColor(Color.WHITE)      // 设置边框颜色
//        if (currentText?.isNotEmpty() == true) {
//            //todo ：这是边框样式
//            mBorderBgRectF.set(
//                x - mPaddingLeft - mPaddingRight,
//                y,
//                x + width + mPaddingRight,
//                y + height + 4
//            )
//        } else {
        //todo ：这是边框样式
        mBorderBgRectF.set(
            x - mPaddingLeft - mPaddingRight,
            y,
            x + width - mPaddingRight-mMargin,
            y + height
        )
//        }
        canvas.drawRoundRect(mBorderBgRectF, height / 2, height / 2, mBorderBgPaint)
    }

    override fun recycle() {
        super.recycle()
        gradientChangeDiggDanmakuDrawItem.recycle()
    }
}

class BorderDanmakuFactory : IDrawItemFactory {

    override fun getDrawType(): Int {
        return DRAW_TYPE_BORDER_TEXT
    }

    override fun generateDrawItem(): DrawItem<out DanmakuData> {
        return BorderDanmakuDrawItem()
    }

}